#!/bin/bash
set -e

# Add +k8s:openapi-gen=true to pkg/apis/harvesterhci.io/v1beta1/doc.go
files=("pkg/apis/harvesterhci.io/v1beta1/doc.go" "vendor/github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1/doc.go" "vendor/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1/doc.go")
for file in "${files[@]}"; do
awk '/package v1/ { print "// +k8s:openapi-gen=true"; print;  next }1' $file > tmp && mv tmp $file;
done

GO111MODULE=auto openapi-gen --input-dirs k8s.io/apimachinery/pkg/util/intstr,k8s.io/apimachinery/pkg/api/resource,k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/runtime,k8s.io/api/core/v1,k8s.io/apimachinery/pkg/api/errors,github.com/openshift/api/operator/v1,kubevirt.io/api/core/v1,kubevirt.io/client-go/apis/snapshot/v1alpha1,kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1,kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1alpha1,github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1,github.com/harvester/harvester/pkg/apis/harvesterhci.io/v1beta1,github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1 \
    --output-package github.com/harvester/harvester/pkg/apis/harvesterhci.io/v1beta1 \
    --go-header-file scripts/boilerplate.go.txt > scripts/known-api-rule-violations.txt

# cleanup +k8s:openapi-gen=true in vendor files.
git checkout -- "${files[@]}"

# generate swagger.json
go run pkg/genswagger/main.go

